package com.puboot.module.controller;

import com.puboot.module.model.LkCategory;
import com.puboot.module.model.LkChapter;
import com.puboot.module.model.LkComic;
import com.puboot.module.service.LkCategoryService;
import com.puboot.module.service.LkChapterService;
import com.puboot.module.service.LkComicService;
import lombok.AllArgsConstructor;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * (LkCategory)表控制层
 *
 * @author Leonidax
 * @since 2024-10-11 16:05:27
 */
@Controller
@AllArgsConstructor
@RequestMapping("user")
public class UserController {
    private final LkCategoryService categoryService;
    private final LkComicService comicService;
    private final LkChapterService chapterService;
    private String initCacheModel(Model model){
        //获取当前语言
        Locale locale = LocaleContextHolder.getLocale();
        String language = locale.getLanguage();
        //分类列表
        List<LkCategory> cates = categoryService.selectCategories();
        model.addAttribute("cates",cates);
        model.addAttribute("lang",language);
        return language;
    }

    @GetMapping("/{id}")
    public String comic(Model model, @PathVariable Integer id){
        String language = initCacheModel(model);
        LkComic comic = comicService.queryById(id,language);
        model.addAttribute("comic",comic);
        List<LkChapter> list = chapterService.chapters(id, language);
        model.addAttribute("list",list);
        model.addAttribute("firstId",list.get(list.size()-1).getId());
        List<LkComic> random = comicService.random(id,3,language);
        model.addAttribute("random",random);
        return "index/comic";
    }

    @GetMapping("/more/{id}")
    public String more(Model model, @PathVariable Integer id){
        String language = initCacheModel(model);
        LkComic comic = comicService.queryById(id,language);
        model.addAttribute("comic",comic);
        List<LkChapter> list = chapterService.chapters(id, language);
        model.addAttribute("list",list);
        model.addAttribute("firstId",list.get(list.size()-1).getId());
        return "index/comic-more";
    }
    @GetMapping("/chapter/{comicId}/{id}")
    public String chapter(Model model, @PathVariable Integer comicId, @PathVariable Integer id){
        String language = initCacheModel(model);
        LkComic comic = comicService.queryById(comicId,language);
        model.addAttribute("comic",comic);
        LkChapter chapter = chapterService.queryById(id,language);
        List<String> imgs = Arrays.stream(chapter.getContent().split(",")).collect(Collectors.toList());
        model.addAttribute("imgs",imgs);
        chapter.setContent(null);
        model.addAttribute("chapter",chapter);
        List<LkChapter> list = chapterService.chapters(comicId, language);
        int size = list.size() - 1;
        model.addAttribute("firstId",list.get(size).getId());
        HashMap<String,String> page = new HashMap<>();
        Integer nextId = 0;
        Integer prevId = 0;
        boolean record = false;
        for(int i=size; i>=0; i--){
            if(list.get(i).getId() < id){
                prevId = list.get(i).getId();
            }
            if(record){
                nextId = list.get(i).getId();
                break;
            }
            if(list.get(i).getId().equals(id)){
                record = true;
            }
        }
        if(nextId == 0){
            page.put("nextClass","background:#e0e0e0;");
            page.put("nextUrl","javascript:;");
        }else{
            page.put("nextClass","background:#ff5a49;");
            page.put("nextUrl","/comic/chapter/"+comicId+"/"+nextId);
        }
        if(prevId == 0){
            page.put("prevClass","background:#e0e0e0;");
            page.put("prevUrl","javascript:;");
        }else{
            page.put("prevClass","background:#ff5a49;");
            page.put("prevUrl","/comic/chapter/"+comicId+"/"+prevId);
        }
        model.addAttribute("page",page);
        List<LkComic> random = comicService.random(comicId,3,language);
        model.addAttribute("random",random);
        return "index/chapter";
    }

}

